#!/bin/bash

usage() {
  echo "Usage: $0 -v <vlan_id> -d <dial_nic> -p <ppp_nic> [-m <mac_addr>]"
  exit 1
}

while getopts ":v:d:p:m:" opt; do
  case $opt in
    v) vlan_id="$OPTARG";;
    d) dial_nic="$OPTARG";;
    p) ppp_nic="$OPTARG";;
    m) mac_addr="$OPTARG";;
    \?) usage;;
  esac
done

if [ -z "$vlan_id" ] || [ -z "$dial_nic" ] || [ -z "$ppp_nic" ]; then
  usage
fi

unit_num=${ppp_nic#ppp}
unit_num=${unit_num#0}

ip link add link $dial_nic name ${dial_nic}.${vlan_id} type vlan id $vlan_id
ip link set ${dial_nic}.${vlan_id} up

ip link add link ${dial_nic}.${vlan_id} name mv-${vlan_id} type macvlan mode bridge
if [ -n "$mac_addr" ]; then
  ip link set mv-${vlan_id} address $mac_addr
fi
ip link set mv-${vlan_id} up

pppd plugin /usr/lib64/pppd/2.4.5/rp-pppoe.so nic-mv-${vlan_id} noipdefault noauth default-asyncmap hide-password nodetach mtu 1500 mru 1500 noaccomp +ipv6 nodeflate nopcomp novj novjccomp user 637143002842 lcp-echo-interval 20 lcp-echo-failure 3 lcp-echo-failure 3 unit $unit_num debug